from django.db import models
from django.contrib.auth.models import Group
from django.contrib.auth.models import User
from enum import Enum
import json
# Create your models here.
class SupplierTypeChoice(Enum):
    FR = "水果"
    PO = "家禽"
    SE = "水产"
    VE = "蔬菜"
    EG = "蛋类"
    OR = "其他"

    def to_json(cls):
        data = []
        for key, value in cls.__members__.items():
            data.append({
                'name': key,
                'value': value.value
            })
        return data

class Buyer(models.Model):
    """
    社区用户,增加手机，地址，是否审核密，微信openid
    """
    user = models.OneToOneField(User, related_name="buyer", on_delete=models.CASCADE)
    phone = models.CharField(max_length=11, verbose_name='手机号', unique=True)
    openid = models.CharField(max_length=64, verbose_name='微信id', null=True, blank=True)
    address = models.CharField(max_length=256, verbose_name='地址', null=True, blank=True)
    province = models.CharField(max_length=64, verbose_name='省', null=True, blank=True)
    city = models.CharField(max_length=8, verbose_name='城市', null=True, blank=True)
    zone = models.CharField(max_length=8, verbose_name='区', null=True, blank=True)
    street = models.CharField(max_length=16, verbose_name='镇街', null=True, blank=True)
    is_verify = models.BooleanField(verbose_name='审核', default=False)
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.phone

    def get_full_name(self):
        """
        Return the first_name plus the last_name, with a space in between.
        """
        full_name = '%s%s' % (self.user.last_name, self.user.first_name)
        return full_name.strip()

    @property
    def full_name(self):
        return self.get_full_name()

    @property
    def nikename(self):
        return self.user.username

    @property
    def email(self):
        return self.user.email


class Supplier(models.Model):
    """
    商户,增加手机，地址，是否审核密，微信openid，企业名字，类别
    """
    user = models.OneToOneField(User, related_name="Supplier", on_delete=models.CASCADE)
    phone = models.CharField(max_length=11, verbose_name='手机号', unique=True)
    company_name = models.CharField(max_length=128, verbose_name='企业名字')
    openid = models.CharField(max_length=64, verbose_name='微信id',null=True, blank=True)
    province = models.CharField(max_length=64, verbose_name='省', null=True, blank=True)
    city = models.CharField(max_length=8, verbose_name='城市', null=True, blank=True)
    zone = models.CharField(max_length=8, verbose_name='区', null=True, blank=True)
    street = models.CharField(max_length=16, verbose_name='镇街', null=True, blank=True)
    address = models.CharField(max_length=64, verbose_name='地址', null=True, blank=True)
    is_verify = models.BooleanField(verbose_name='审核', default=False)
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.phone

    def get_full_name(self):
        """
        Return the first_name plus the last_name, with a space in between.
        """
        full_name = '%s%s' % (self.user.last_name, self.user.first_name)
        return full_name.strip()

    @property
    def boss_name(self):
        return self.get_full_name()

    @property
    def nikename(self):
        return self.user.username

    @property
    def email(self):
        return self.user.email


class Item(models.Model):
    """可选需求项目"""
    name = models.CharField(max_length=64, verbose_name='名称')
    content = models.TextField(verbose_name='描述')
    sup_type = models.CharField(
        max_length=2,
        verbose_name='类别',
        choices=[(tag.name, tag.value) for tag in SupplierTypeChoice],
        default=SupplierTypeChoice.OR.name
    )
    is_deleted = models.BooleanField(verbose_name='无效', default=False)
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name


class Order(models.Model):
    """需求表单"""
    buyer = models.ForeignKey(Buyer, related_name="orders", on_delete=models.CASCADE, )
    content = models.TextField(verbose_name='描述json')
    is_verify = models.BooleanField(verbose_name='审核', default=False)
    is_deleted = models.BooleanField(verbose_name='无效', default=False)
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f'{self.buyer.phone}:{self.created.strftime("%Y/%m/%d")}'

    @property
    def buyer_name(self):
        return self.buyer.full_name

    @property
    def buyer_phone(self):
        return self.buyer.phone

    @property
    def buyer_address(self):
        return f'{self.buyer.zone}{self.buyer.street}{self.buyer.address}'

    @property
    def buyer_zone(self):
        return self.buyer.zone

    @property
    def content_plus(self):
        content = None
        try:
            content = json.loads(self.content)
        except Exception as e:
            print(e)
            content = self.content
        finally:
            return content

class OrderItem(models.Model):
    """需求表单项内容"""
    order = models.ForeignKey(Order, related_name="order_items", on_delete=models.CASCADE, )
    item = models.ForeignKey(Item, related_name="the_item", on_delete=models.CASCADE, )
    count = models.IntegerField(verbose_name="数量")

    def __str__(self):
        return f'{self.order.id}-{self.item.name}:{self.count}'

class goodItem(models.Model):
    """商品项目表"""
    supplier = models.ForeignKey(Supplier, related_name="good", on_delete=models.CASCADE, )
    name = models.CharField(max_length=64, verbose_name='名称')
    content = models.TextField(verbose_name='描述json')
    sup_type = models.CharField(
        max_length=2,
        verbose_name='类别',
        choices=[(tag.name, tag.value) for tag in SupplierTypeChoice],
        default=SupplierTypeChoice.OR.name
    )
    is_verify = models.BooleanField(verbose_name='审核', default=False)
    is_deleted = models.BooleanField(verbose_name='无效', default=False)
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

    @property
    def supplier_name(self):
        return self.supplier.full_name

    @property
    def supplier_company_name(self):
        return self.supplier.company_name

    @property
    def supplier_phone(self):
        return self.supplier.phone

    @property
    def supplier_address(self):
        return f'{self.supplier.zone}{self.supplier.street}{self.supplier.address}'

    @property
    def supplier_zone(self):
        return self.supplier.zone

    @property
    def content_plus(self):
        content = None
        try:
            content = json.loads(self.content)
        except Exception as e:
            print(e)
            content = self.content
        finally:
            return content

class goodVerfiyImage(models.Model):
    """商品图片表"""
    good = models.ForeignKey(goodItem, related_name="good_img", on_delete=models.CASCADE, )
    name = models.CharField(max_length=64, verbose_name='名称')
    my_img = models.ImageField(verbose_name='商品图片', upload_to="%Y/%m/%d/")
    is_verify = models.BooleanField(verbose_name='审核', default=False)
    is_deleted = models.BooleanField(verbose_name='无效', default=False)
    created = models.DateTimeField(auto_now_add=True)